var express = require('express');
var router = express.Router();

//引入Query方法，执行sql语句 
const Query = require('../utils/db.js')
//使用公共数据
let { api_url } = require('../utils/config.js')

/* GET home page. */
router.get('/', async function (req, res, next) {
    res.send('课程详情')
});


router.get('/course', async function (req, res, next) {

    // 接收所有的参数
    let { id = '' } = req.query

    let sql1 = `
    SELECT cid,title,CONCAT('${api_url}/uploads/courses/',detail_imgs) AS detail_imgs, CONCAT('${api_url}/uploads/courses/',image_src) AS image_src,intro,type_name,area_name,category_name,subject_name,grade_name,price,intro,tid FROM e_course WHERE cid='${id}' ORDER BY id DESC
    ` //返回具体课程列表的sql
    let res1 = await Query(sql1) //返回具体课程列表
    let a = res1.result[0]


    let sql2 = ` SELECT id,tid,realname,intro,CONCAT('${api_url}/uploads/courses/',head_photo_url) AS head_photo_url FROM e_teacher  WHERE tid='${a.tid}'` //老师
    let res2 = await Query(sql2) //老师信息
    let b = res2.result[0]

    let sql3 = `SELECT id,vnum,video_title FROM e_video WHERE cid='${id}'`//视频 
    let res3 = await Query(sql3) //视频
    let c = res3.result

    console.log(a)
    

    let data = { //要返回前端的json数据
        status: 200,
        msg: "OK",
        result: {
            course:a,
            teacher:b,
            videos:c
        }
    }
    res.send(data)
});



module.exports = router;
